<script setup lang="ts">
import { RouterView, useRouter } from 'vue-router'
import {
  ElContainer,
  ElHeader,
  ElMain,
  ElFooter,
  ElMenu,
  ElMenuItem,
  ElButton,
  ElDropdown,
  ElDropdownMenu,
  ElDropdownItem,
  ElInput,
  ElIcon,
} from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { useAppStore } from '@/stores/app'
import { ref, onMounted } from 'vue'

// 在组件中获取 store 实例
const userStore = useUserStore()
const appStore = useAppStore()
const router = useRouter()
const searchQuery = ref('')

// 初始化应用
onMounted(async () => {
  // 初始化应用状态
  appStore.initApp()

  // 初始化用户状态
  await userStore.initUserState()
})

// 处理搜索
const handleSearch = () => {
  if (searchQuery.value.trim()) {
    router.push(`/search?q=${searchQuery.value.trim()}`)
    searchQuery.value = ''
  }
}

// 处理登录
const handleLogin = () => {
  router.push('/login')
}

// 处理注册
const handleRegister = () => {
  router.push('/register')
}

// 处理登出
const handleLogout = async () => {
  await userStore.logout()
  router.push('/')
}
</script>

<template>
  <el-container class="main-layout">
    <el-header class="main-header">
      <div class="logo">积分商城</div>
      <el-menu mode="horizontal" :router="true" default-active="/" class="nav-menu">
        <el-menu-item index="/">首页</el-menu-item>
        <el-menu-item index="/products">商品列表</el-menu-item>
        <el-menu-item index="/about">关于我们</el-menu-item>
      </el-menu>

      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="搜索商品"
          @keyup.enter="handleSearch"
        >
          <template #append>
            <el-button @click="handleSearch"><el-icon><Search /></el-icon></el-button>
          </template>
        </el-input>
      </div>

      <!-- 用户信息区 -->
      <div class="user-info">
        <template v-if="userStore.isAuthenticated">
          <el-dropdown>
            <span class="user-dropdown">
              <span>欢迎, {{ userStore.username }}</span>
              <span class="points">积分: {{ userStore.points }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item v-if="userStore.isAdmin" @click="router.push('/admin')">后台管理</el-dropdown-item>
                <el-dropdown-item v-if="userStore.isAdmin" divided />
                <el-dropdown-item @click="router.push('/profile')">个人中心</el-dropdown-item>
                <el-dropdown-item @click="router.push('/orders')">我的订单</el-dropdown-item>
                <el-dropdown-item @click="router.push('/points')">积分中心</el-dropdown-item>
                <el-dropdown-item @click="router.push('/addresses')">收货地址</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
        <template v-else>
          <el-button @click="handleLogin" type="primary">登录</el-button>
          <el-button @click="handleRegister" type="default">注册</el-button>
        </template>
      </div>
    </el-header>

    <el-main class="main-content">
      <!-- 路由视图被包裹在 Main 区域中 -->
      <RouterView />
    </el-main>
    <el-footer class="main-footer">
      © 2025 PMall. All Rights Reserved.
    </el-footer>
  </el-container>
</template>

<style scoped>
.main-layout {
  min-height: 100vh;
}

.main-header {
  display: flex;
  align-items: center;
  background-color: #ffffff;
  border-bottom: 1px solid #ebeef5;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  margin-right: 50px;
}

/* 我们需要让菜单占据剩余空间，以便后续可以添加右侧的用户信息 */
.nav-menu {
  flex-grow: 1;
  border-bottom: none;
  /* 去掉菜单默认的下边框 */
}

.main-content {
  background-color: #f4f4f5;
  padding: 20px;
}

.main-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ffffff;
  color: #909399;
  font-size: 14px;
  border-top: 1px solid #ebeef5;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f7fa;
}

.points {
  color: #e6a23c;
  font-weight: 500;
}
</style>
